{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name: bee01 \t image resolution: 600X720X3 \t  frame num: 665 \t maximum bee in scene is 17 \t  ano num: 7185 \t  \n",
      "name: bee02 \t image resolution: 720X1280X3 \t  frame num: 274 \t maximum bee in scene is 12 \t  ano num: 2108 \t  \n",
      "name: bee03 \t image resolution: 720X1280X3 \t  frame num: 174 \t maximum bee in scene is 15 \t  ano num: 1561 \t  \n",
      "name: bee04 \t image resolution: 192X262X3 \t  frame num: 163 \t maximum bee in scene is 11 \t  ano num: 1569 \t  \n",
      "name: bee05 \t image resolution: 720X1280X3 \t  frame num: 434 \t maximum bee in scene is 21 \t  ano num: 7846 \t  \n",
      "name: bee06 \t image resolution: 720X1280X3 \t  frame num: 434 \t maximum bee in scene is 26 \t  ano num: 9757 \t  \n",
      "name: bee07 \t image resolution: 720X1280X3 \t  frame num: 1131 \t maximum bee in scene is 31 \t  ano num: 30446 \t  \n"
     ]
    }
   ],
   "source": [
    "import os \n",
    "from os.path import  exists, join, split, isdir,isfile \n",
    "import glob\n",
    "import shutil\n",
    "import numpy as np\n",
    "import cv2\n",
    "import collections\n",
    "\n",
    "class  BeeDataChecker:\n",
    "    \n",
    "    def __init__(self,path):\n",
    "        \n",
    "        self.root = path \n",
    "        self.sequence_list = sorted(glob.glob(path + '/bee*'))\n",
    "        \n",
    "\n",
    "    def stat(self,idx):\n",
    "        cur_path = self.sequence_list[idx]\n",
    "        label_path = join(cur_path,'gt','gt.txt')\n",
    "        data = np.loadtxt(label_path,np.str0)\n",
    "        gt = [x.split(',') for x in data]\n",
    "        gt = np.array(gt)\n",
    "        \n",
    "        demo_img = cv2.imread(join(cur_path,'img1','%06d.jpg'%(1)))\n",
    "\n",
    "        \n",
    "        frame_num = len(set(gt[:,0]))\n",
    "        anno_num = gt.shape[0]\n",
    "        name = self.sequence_list[idx].split('/')[-1]\n",
    "\n",
    "        \n",
    "        counter = collections.Counter(gt[:,0])\n",
    "        max_key = max(counter, key=lambda k: counter[k])\n",
    "        # print(\"键值最大的键是:\", max_key, \"value is :%d\"%(counter[max_key]))\n",
    "\n",
    "        \n",
    "\n",
    "        print(\"name: %s \\t image resolution: %s \\t  frame num: %d \\t maximum bee in scene is %d \\t  ano num: %d \\t  \"%(name,\n",
    "        'X'.join([str(x) for x in  demo_img.shape]), \n",
    "          frame_num, counter[max_key], anno_num))\n",
    "        \n",
    "        \n",
    "        \n",
    "    def __len__(self,):\n",
    "        \n",
    "        return len(self.sequence_list)\n",
    "\n",
    "    def check_id(self,idx):\n",
    "        cur_path = self.sequence_list[idx]\n",
    "        label_path = join(cur_path,'gt','gt.txt')\n",
    "\n",
    "        data = np.loadtxt(label_path,np.str0)\n",
    "\n",
    "\n",
    "\n",
    "        gt = [x.split(',') for x in data]\n",
    "        gt = np.array(gt)\n",
    "        dict_gt = {}\n",
    "        for line in gt:\n",
    "            if dict_gt.get(line[0]) is None:\n",
    "                dict_gt[line[0]] = [line[1:].astype(np.float32)]\n",
    "            else:\n",
    "                dict_gt[line[0]].append(line[1:].astype(np.float32))\n",
    "\n",
    "        for k,v in dict_gt.items():\n",
    "\n",
    "            all_idx = [ x[0] for x in v]\n",
    "            if len(all_idx) != len(set(all_idx)):\n",
    "                print('error happened in ', k)\n",
    "\n",
    "        \n",
    "        return dict_gt\n",
    "\n",
    "\n",
    "\n",
    "    def modify_gt(self,idx):\n",
    "        cur_path = self.sequence_list[idx]\n",
    "\n",
    "        label_path = join(cur_path,'gt','gt.txt')\n",
    "\n",
    "        \n",
    "        data = np.loadtxt(label_path,np.str0)\n",
    "        new_data = [] \n",
    "        for line in data :\n",
    "            # print('origin : ',line)\n",
    "            eles = line.split(',')\n",
    "            # print(eles)\n",
    "            modified_frame_id = str(int(eles[0])+1).rjust(6, '0')\n",
    "            eles.pop(0)\n",
    "            eles.insert(0, modified_frame_id)\n",
    "            after_modifted = ','.join(eles)\n",
    "\n",
    "            # print('after : ',after_modifted)\n",
    "            new_data.append(after_modifted)\n",
    "        # print(new_data)\n",
    "        np.savetxt(label_path,new_data,fmt='%s')\n",
    "\n",
    "    def modify_minus_coord(self,idx):\n",
    "        cur_path = self.sequence_list[idx]\n",
    "\n",
    "        label_path = join(cur_path,'gt','gt.txt')\n",
    "\n",
    "        data = np.loadtxt(label_path,np.str0)\n",
    "        new_data = [] \n",
    "        modified_lines = 0 \n",
    "        for line in data :\n",
    "            # print('origin : ',line)\n",
    "            eles = line.split(',')\n",
    "            # print(eles)\n",
    "            new_eles = eles.copy()\n",
    "            modified = False\n",
    "            for x in range(2,6):\n",
    "                if int(new_eles[x]) < 0:\n",
    "                    new_eles[x]= \"0\"\n",
    "                    modified = True\n",
    "            if modified:\n",
    "                modified_lines +=1\n",
    "\n",
    "                    \n",
    "                \n",
    "            after_modifted = ','.join(new_eles)\n",
    "            # print('after : ',after_modifted)\n",
    "            new_data.append(after_modifted)\n",
    "\n",
    "        print('modified_lines: %d'%modified_lines)\n",
    "        np.savetxt(label_path,new_data,fmt='%s')\n",
    "    \n",
    "    \n",
    "\n",
    "# checker = BeeDataChecker(\"/data/xusc/exp/topictrack-bee/data/beedance/test\")\n",
    "# checker = BeeDataChecker(\"/data/xusc/exp/topictrack-bee/data/beedance/train\")\n",
    "checker = BeeDataChecker(\"/data/xusc/exp/topictrack-bee/data/beedance/beedance\")\n",
    "\n",
    "# checker.modify_minus_coord(0)\n",
    "\n",
    "# for x in range(0,3):\n",
    "#     print(x)\n",
    "#     checker.modify_minus_coord(x)\n",
    "\n",
    "# for x in range(checker.__len__()):\n",
    "\n",
    "#     checker.check_id(x)\n",
    "\n",
    "for i in range(checker.__len__()):\n",
    "\n",
    "    checker.stat(i)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gt.shape\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_gt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[0],len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# file_path = \"/data/xusc/exp/topictrack-bee/data/beedance/test/bee02/gt/gt.txt\"  # 替换为实际的文件路径\n",
    "file_path = \"/data/xusc/exp/topictrack-bee/data/beedance/train/bee04/gt/gt.txt\"  # 替换为实际的文件路径\n",
    "\n",
    "column_counts = [0] * 6  # 用于统计每一列负数的计数器，初始化为全零\n",
    "\n",
    "with open(file_path, 'r') as file:\n",
    "    for line in file:\n",
    "        line = line.strip()  # 去除行尾的换行符和空格\n",
    "        columns = line.split(\",\")  # 按逗号分隔每一列数据\n",
    "        \n",
    "        for i in range(2, 8):\n",
    "            value = int(columns[i])\n",
    "            if value < 0:\n",
    "                column_counts[i-2] += 1\n",
    "\n",
    "print(\"第2列负数数目：\", column_counts[0])\n",
    "print(\"第3列负数数目：\", column_counts[1])\n",
    "print(\"第4列负数数目：\", column_counts[2])\n",
    "print(\"第5列负数数目：\", column_counts[3])\n",
    "print(\"第6列负数数目：\", column_counts[4])\n",
    "print(\"第7列负数数目：\", column_counts[5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "    \n",
    "label_path = \"/data/xusc/exp/topictrack-bee/results/gt/beedance-val/bee02/gt/gt.txt\"\n",
    "\n",
    "data = np.loadtxt(label_path,np.str0)\n",
    "new_data = [] \n",
    "for line in data :\n",
    "    # print('origin : ',line)\n",
    "    eles = line.split(',')\n",
    "    # print(eles)\n",
    "    modified_frame_id = str(int(eles[0])+1).rjust(6, '0')\n",
    "    eles.pop(0)\n",
    "    eles.insert(0, modified_frame_id)\n",
    "    after_modifted = ','.join(eles)\n",
    "\n",
    "    # print('after : ',after_modifted)\n",
    "    new_data.append(after_modifted)\n",
    "# print(new_data)\n",
    "np.savetxt(label_path,new_data,fmt='%s')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name: ant01 \t image resolution: 720X1280X3 \t  frame num: 270 \t maximum bee in scene is 12 \t  ano num: 2719 \t  \n",
      "name: ant02 \t image resolution: 720X1280X3 \t  frame num: 838 \t maximum bee in scene is 7 \t  ano num: 4845 \t  \n",
      "name: ant03 \t image resolution: 720X1280X3 \t  frame num: 561 \t maximum bee in scene is 9 \t  ano num: 3049 \t  \n",
      "name: ant04 \t image resolution: 720X1280X3 \t  frame num: 700 \t maximum bee in scene is 4 \t  ano num: 2539 \t  \n",
      "name: ant05 \t image resolution: 720X1280X3 \t  frame num: 303 \t maximum bee in scene is 12 \t  ano num: 2964 \t  \n",
      "name: ant06 \t image resolution: 720X1280X3 \t  frame num: 259 \t maximum bee in scene is 13 \t  ano num: 2296 \t  \n",
      "name: ant07 \t image resolution: 720X1280X3 \t  frame num: 576 \t maximum bee in scene is 6 \t  ano num: 2355 \t  \n",
      "name: ant08 \t image resolution: 720X1280X3 \t  frame num: 262 \t maximum bee in scene is 9 \t  ano num: 2037 \t  \n",
      "name: ant09 \t image resolution: 480X720X3 \t  frame num: 938 \t maximum bee in scene is 20 \t  ano num: 16552 \t  \n"
     ]
    }
   ],
   "source": [
    "import os \n",
    "from os.path import  exists, join, split, isdir,isfile \n",
    "import glob\n",
    "import shutil\n",
    "import numpy as np\n",
    "import cv2\n",
    "import collections\n",
    "\n",
    "class  BeeDataChecker:\n",
    "    \n",
    "    def __init__(self,path):\n",
    "        \n",
    "        self.root = path \n",
    "        self.sequence_list = sorted(glob.glob(path + '/ant*'))\n",
    "        \n",
    "\n",
    "    def stat(self,idx):\n",
    "        cur_path = self.sequence_list[idx]\n",
    "        label_path = join(cur_path,'gt','gt.txt')\n",
    "        data = np.loadtxt(label_path,np.str0)\n",
    "        gt = [x.split(',') for x in data]\n",
    "        gt = np.array(gt)\n",
    "        \n",
    "        demo_img = cv2.imread(join(cur_path,'img1','%06d.jpg'%(1)))\n",
    "\n",
    "        \n",
    "        frame_num = len(set(gt[:,0]))\n",
    "        anno_num = gt.shape[0]\n",
    "        name = self.sequence_list[idx].split('/')[-1]\n",
    "\n",
    "        \n",
    "        counter = collections.Counter(gt[:,0])\n",
    "        max_key = max(counter, key=lambda k: counter[k])\n",
    "        # print(\"键值最大的键是:\", max_key, \"value is :%d\"%(counter[max_key]))\n",
    "\n",
    "        \n",
    "\n",
    "        print(\"name: %s \\t image resolution: %s \\t  frame num: %d \\t maximum bee in scene is %d \\t  ano num: %d \\t  \"%(name,\n",
    "        'X'.join([str(x) for x in  demo_img.shape]), \n",
    "          frame_num, counter[max_key], anno_num))\n",
    "        \n",
    "        \n",
    "        \n",
    "    def __len__(self,):\n",
    "        return len(self.sequence_list)\n",
    "\n",
    "\n",
    "checker = BeeDataChecker(\"/data/xusc/exp/topictrack-bee/data/antmove/all_antmove\")\n",
    "\n",
    "for i in range(checker.__len__()):\n",
    "\n",
    "    checker.stat(i)\n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yang_real",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
